package com.ybfenpan.controller.mall;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.ybfenpan.model.Member;
import com.ybfenpan.model.mall.MyAddress;
import com.ybfenpan.model.shop.Goods;
import com.ybfenpan.service.MemberService;
import com.ybfenpan.service.mall.MyAddressService;
import com.ybfenpan.service.shop.GoodsService;
import com.ybfenpan.taglib.pagetag.Pagination;
import com.ybfenpan.utils.ContextUtil;
import com.ybfenpan.utils.MapUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ybfenpan.controller.core.BaseController;

@Controller
public class HomeController extends BaseController {

	@Resource 
	private MemberService memberService;

	
	@Resource 
	private GoodsService goodsService;
	@Resource 
	private MyAddressService myAddressService;
	
	 
	@RequestMapping("/mall/home.shtml")
	public String memberLogin(HttpServletRequest request, HttpSession session, HttpServletResponse response, Model model) {
		
			HashMap formMap = getParameterMap(request);
			formMap.put("isActive", "T");	
	        formMap.put("where", "1=1 and totalNum> 0 ");	
	        formMap.put("pagination.orderBy","  id desc");  //排列
	        Pagination page = goodsService.queryForPageList(formMap);
	        
	        request.setAttribute(Pagination.pageBeanName, page);
		
		return "/mall/home";
	}
	
	@RequestMapping("/mall/productDetailed.shtml")
	public String shopDetail(HttpServletRequest request, HttpSession session, HttpServletResponse response, Model model) { 
	
		Map formMap = getParameterMap(request);
        int id = MapUtil.getIntValue(formMap, "id");
        if(0 < id){
        	Goods goods = goodsService.getByKey(id);
        	if(goods.getIsActive().equals("T")){
        		request.setAttribute("goods",goods);
        		request.setAttribute("id",id);
                
        	}  
    	
        }
		
		return "/mall/productDetailed";
	}
	
	@RequestMapping("/mall/cartAll.shtml")
	public String cartAll(HttpServletRequest request, HttpSession session, HttpServletResponse response, Model model) { 
	
		return "/mall/cartAll";
	}
	
	@RequestMapping("/mall/placeOrder.shtml")
	public String placeOrder(HttpServletRequest request, HttpSession session, HttpServletResponse response, Model model) { 
			HashMap formMap = getParameterMap(request);
			//代理商信息
			Member member = ContextUtil.getMember();
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("memberId", member.getId());
			List<MyAddress> members = myAddressService.queryForList("select", param);
			
			if (members!=null&&members.size()>0) {
				MyAddress myAddress=members.get(0);
				if (myAddress!=null) {
					request.setAttribute("myAddress",myAddress);
				}
			}
			
	        int id = MapUtil.getIntValue(formMap, "id");
	        if(0 < id){
	        	Goods goods = goodsService.getByKey(id);
	        	if(goods.getIsActive().equals("T")){
	        		request.setAttribute("goods",goods);
	        	}  
	    	
	        }
		
		return "/mall/placeOrder";
	}
	
}
